﻿package com.hb.ssmdemo.tools.udp;

import java.util.ArrayList;
import java.nio.ByteBuffer;

/**
 * 
 *@ClassName:DeCode
 *@Description:UTP规约转换 (工具类)
 *
 **/
public class DeCode {
	private static String hexString = "0123456789ABCDEF";

	private static byte toByte(char c) {
		byte b = (byte) hexString.indexOf(c);
		return b;
	}

	/**
	 * 
	* @Description:int型数组转化为16进制字符串
	* @param bArray
	* @return
	 */
	public static final String intToHexString(int[] bArray) {
		StringBuffer sb = new StringBuffer(bArray.length);
		String sTemp;
		for (int i = 0; i < bArray.length; i++) {
			sTemp = Integer.toHexString(0xFFFF&bArray[i]);
			if (sTemp.length() < 2)
				sb.append(0);
			sb.append(sTemp.toUpperCase());
		}
		return sb.toString();
	}
	
	/**
	 * 
	* @Description:十六进制字符串（8位）转换为浮点型（高位在前，低位在后）
	* @param str
	* @return
	 */
	public static final float HexStringToFloat(String str) {
		int bii = Integer.parseInt(str, 16);
		float a11 = Float.intBitsToFloat(bii);
		//System.out.println(a11);
		return a11;
	}

	/**
	 * 
	* @Description:数值反转（高低位交换）
	* @param data
	* @return
	 */
	public static byte[] dataValueRollback(byte[] data) {
		ArrayList<Byte> al = new ArrayList<Byte>();
		for (int i = data.length - 1; i >= 0; i--) {
			al.add(data[i]);
		}
		byte[] buffer = new byte[al.size()];
		for (int i = 0; i <= buffer.length - 1; i++) {
			buffer[i] = al.get(i);
		}
		return buffer;
	}

	/**
	 * 
	* @Description:float转换为字符串
	* @param buffer
	* @return
	 */
	public static final String floatTohexString(float buffer) {
		ByteBuffer bbuf = ByteBuffer.allocate(4);
		bbuf.putFloat(buffer);
		byte[] bBuffer = bbuf.array();
		bBuffer = dataValueRollback(bBuffer);
		String result = bytesToHexString(bBuffer);
		return result;
	}
	
	/**
	 * 
	* @Description:两个字节数组到整数的转换
	* @param b
	* @return
	 */
	public static int bytes2int(byte b[]) {
		int s = 0;
		s = (b[1] & 0xff) << 8 | (b[0] & 0xff);
		return s;
	}

	/**
	 * 
	* @Description:short型转换为两个十六进制字节
	* @param n
	* @return
	 */
	public static byte[] short2bytes(short n) {
		byte[] ab = new byte[2];
		ab[0] = (byte) (0xff & n);
		ab[1] = (byte) ((0xff00 & n) >> 8);
		return ab;
	}

	/**
	 * 
	* @Description:short型转化为两个十六进制字符串（低字节在前，高字节在后）
	* @param n
	* @return
	 */
	public static String short2String(short n) {
		byte[] ab = new byte[2];
		ab[0] = (byte) (0xff & n);
		ab[1] = (byte) ((0xff00 & n) >> 8);
		String string = bytesToHexString(ab);
		return string;

	}

	/**
	 * 
	* @Description:十六进制字符串转化为10进制数组（两个十六进制字符串组成一个字节，然后转换为10进制,范围为0~255）
	* @param hex
	* @return
	 */
	public static int[] hexStringToByte1(String hex) {
		int len = (hex.length()) / 2;
		int[] result = new int[len];
		char[] achar = hex.toCharArray();
		for (int i = 0; i < len; i++) {
			int pos = i * 2;
			result[i] = (int) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));

			//	System.out.println(result[i]);
		}
		return result;
	}

	/**
	 * 
	* @Description:十六进制字符串转化为10进制数组（两个十六进制字符串组成一个字节，然后转换为10进制，范围为-127~127）
	* @param hex
	* @return
	 */
	public static byte[] hexStringToByte(String hex) {
		int len = (hex.length()) / 2;
		byte[] result = new byte[len];
		char[] achar = hex.toCharArray();
		for (int i = 0; i < len; i++) {
			int pos = i * 2;
			result[i] = (byte) (toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
		}
		return result;
	}

	/**
	 * 
	* @Description:字节数组转换为十六进制字符串
	* @param bArray
	* @return
	 */
	public static final String bytesToHexString(byte[] bArray) {
		StringBuffer sb = new StringBuffer(bArray.length);
		String sTemp;
		for (int i = 0; i < bArray.length; i++) {
			sTemp = Integer.toHexString(0xFF & bArray[i]);
			if (sTemp.length() < 2)
				sb.append(0);
			sb.append(sTemp.toUpperCase());
		}
		return sb.toString();
	}

	/**
	 * 
	* @Description:8位十六进制字符串转换为浮点型（低位在前 ，高位在后）
	* @param strLH
	* @return
	 */
	public static final float hexStringToFloat(String strLH) {
		byte[] LH = hexStringToByte(strLH);
		byte[] HL = dataValueRollback(LH);
		String strHL = bytesToHexString(HL);
		int bi = Integer.parseInt(strHL, 16);
		float result = Float.intBitsToFloat(bi);
		return result;
	}
	/**
	 * 
	* @Description:(简要描述该方法作用)二进制字符串转换成十六进制
	* @param binaryString
	* @return
	 */
    public static final String binaryToHesString(String binaryString)
    {
    	 int tempStr=Integer.parseInt(binaryString, 2);
 	     String hexStr = intToHexString(new int[]{tempStr});
 	     return hexStr;
    }
	/**
	 * 
	* @Description:将小写字母转化为大写
	* @param str
	* @return
	 */
	public static String convertString(String str) {
		String upStr = str.toUpperCase();
		//String lowStr = str.toLowerCase();
		StringBuffer buf = new StringBuffer(str.length());
		for (int i = 0; i < str.length(); i++) {
			buf.append(upStr.charAt(i));
		}
		return buf.toString();
	}
	
	/**
	 * 
	 * @Description: 将8个十六进制字符串转换为点号间隔的整数
	 * 
	 */
	public static String hexStrToInt(String str){
		
		StringBuffer sb = new StringBuffer();
		String temp;
		for(int i = 0; i < 8; i+=2){
			temp = str.substring(i, i + 2);
			sb.append(Integer.parseInt(temp, 16)).append(".");
		}
		temp = sb.toString();
		temp = temp.substring(0, temp.length() - 1);
		return temp;
	}
	
	/**
	 * 
	 * @Description: 将2个十六进制字符串转换为整数
	 * 
	 */
	public static int twoHexStrToInt(String str){
		return Integer.parseInt(str, 10);
	}
	
	public static void main(String[] agrs){
		int[] bArray = new int[]{17,102,5,231};
		System.out.println(DeCode.intToHexString(bArray));
	}
}
